﻿using AutoMapper;
using MediatR;
using MySqlX.XDevAPI.Common;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Supplier
{
    public class BanksUpdateHandler : IRequestHandler<BanksUpdateCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 银行信息
        /// </summary>
        private readonly IBaseRepository<Banks> banksRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="banksRepository"></param>
        /// <param name="mapper"></param>
        public BanksUpdateHandler(IBaseRepository<Banks> banksRepository, IMapper mapper)
        {
            this.banksRepository = banksRepository;
            this.mapper = mapper;
        }

        public async Task<THE_WIND_RISES.Common.Dtos.Result> Handle(BanksUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Update };
            try
            {
                var AddData = mapper.Map<Banks>(request);
                string sql = "UPDATE Banks SET BanksNo=@BanksNo ,BranchNo=@BranchNo ,BanksAccount=@BanksAccount ,BankStatus =1 WHERE BanksId =@BanksId ";
                var AddNum = banksRepository.Update(sql, new
                {
                    BanksId = AddData.BanksId,
                    BanksNo = AddData.BanksNo,
                    BranchNo = AddData.BranchNo,
                    BanksAccount = AddData.BanksAccount,
                    BankStatus = AddData.BankStatus,

                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
